package com.dobest.aspectdemo;

import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Package: com.dobest.aspectdemo
 * @author: hujiansong
 * @date: 2018/8/7 14:12
 * @version: V1.0
 */
public class EventAsMapDemo {
    private static void handleResult(String resultKey, Map<String, Object> resultMap, Map<String, Object> eventAsMap) {
        if (!StringUtils.isEmpty(resultKey) && !"-1".equals(resultKey.trim())) {
            String resultSubKeys[] = resultKey.split(",");
            for (String resultSubKey : resultSubKeys) {
                String entry[] = resultSubKey.split("=", 2);
                String eventAsMapKey = entry[0];
                String resultMapKey = entry[1];
                Object value = resultMap.get(resultMapKey);
                if (eventAsMapKey.indexOf(".") != -1) {
                    Map<String, Object> subMap = eventAsMap;
                    String eventAsMapSubKeys[] = eventAsMapKey.split("\\.");
                    for (String eventAsMapSubKey : eventAsMapSubKeys) {
                        Object tempObj = subMap.get(eventAsMapSubKey);
                        if (tempObj instanceof Map) {
                            subMap = (Map<String, Object>) tempObj;
                        } else if (subMap != null && subMap != eventAsMap) {
                            subMap.put(eventAsMapSubKey, value);
                        } else {
                        }
                    }
                } else {
                    eventAsMap.put(eventAsMapKey, value);
                }
            }
        }
    }


    public static void main(String[] args) {
        String resultKey = "extends.time=result";
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("result", 1000);


        Map<String, Object> eventAsMap = new HashMap<>();
        eventAsMap.put("app_id", 10050);
        eventAsMap.put("paramters", 10050);
        eventAsMap.put("extends", new HashMap<>());


        handleResult(resultKey,resultMap,eventAsMap);




    }


}
